package com.tpp.service.impl;

import com.tpp.dao.CommentsDao;
import com.tpp.pojo.Comment;
import com.tpp.pojo.CommentReply;
import com.tpp.pojo.baseVo.BaseResult;
import com.tpp.pojo.baseVo.CommentBase;
import com.tpp.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.Date;
import java.util.List;

@Service
public class CommentServiceImpl implements CommentService {
    @Autowired
    CommentsDao commentDao;

    @Override
    public BaseResult addComment(Comment comment) {
        BaseResult baseResult = new BaseResult();
        Integer i = null;
        if (comment != null) {
            Date date = new Date();
            comment.setCommentTime(date);
            i = commentDao.addComment(comment);
            if (i > 0) {
                baseResult.setCode(1);
                baseResult.setMsg("添加成功");
                return baseResult;
            } else {
                baseResult.setCode(0);
                baseResult.setMsg("添加失败");
                return baseResult;
            }
        } else {
            baseResult.setCode(0);
            baseResult.setMsg("老弟，你传过来的数据是空的");
            return baseResult;
        }
    }

    @Override
    public BaseResult deleteComment(Integer id, Integer uid) {
        Integer integer = null; 
        if (id != null && uid != null) {
            if (uid == commentDao.findComment(id)) {
                integer = commentDao.deleteComment(id);
                if (integer > 0) {
                    return new BaseResult(1, "删除成功");
                } else {
                    return new BaseResult(0, "删除失败");
                }
            } else {
                return new BaseResult(0, "你删人家的评论干嘛？");
            }
        } else {
            return new BaseResult(0, "数据没传过来");
        }
    }


    @Override
    public BaseResult findAllComment(Integer mid) {
        List<CommentBase> commentBases = commentDao.findAllComment(mid);
        BaseResult baseResult = new BaseResult();
        baseResult.setCode(0);
        baseResult.setMsg("ok");
        baseResult.setData(commentBases);
        return baseResult;
    }

    @Override
    public BaseResult addCommentReply(CommentReply commentReply) {
        BaseResult baseResult = new BaseResult();
        Integer integer = null;
        if (commentReply != null) {
            Date date = new Date();
            commentReply.setReplyTime(date);
            integer = commentDao.addCommentsReply(commentReply);
            if (integer > 0) {
                return new BaseResult(1, "添加成功", null, null);
            } else {
                return new BaseResult(0, "添加失败", null, null);
            }
        }
        return baseResult;
    }
//删除回复评论
    @Override
    public BaseResult deleteCommentReply(Integer id, Integer uid) {
        Integer integer=null;

        if (id!=null && uid != null) {
            if (uid==commentDao.findCommentReplyById(id)) {
                integer = commentDao.deleteCommentReply(id);
                if (integer > 0) {
                    return new BaseResult(1, "删除成功");
                } else {
                    return new BaseResult(0, "删除失败");
                }
            }else {
                return new BaseResult(0,"你删人家的评论干嘛?");
            }
        }else {
            return new BaseResult(0,"数据没传过来");
        }
    }
}